package com.spring.security.wiremock;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;

import static com.github.tomakehurst.wiremock.client.WireMock.*;

/**
 * @ClassName MockServer
 * @Description MockServer
 * @Author 朱秒
 * @Date 2020/2/21 15:19
 * @Version 1.0
 */
public class MockServer {

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        //首先前提是下载wiremock官网的standalone的jar包，并启动
        configureFor("127.0.0.1",8062);
        removeAllMappings();
        //todo 这里不知道为什么启动后直接运行完了，不维持启动状态。
        stubFor(get(urlPathEqualTo("/order/1")).willReturn(aResponse().withBody("{\"id\":\"111\"}").withStatus(200)));
        //mock("/order/1", "01");
        //mock("/order/2", "02");
    }

    private static void mock(String url, String file) throws IOException {
        ClassPathResource resource = new ClassPathResource("mock/response/" + file + ".txt");
        String content = StringUtils.join(FileUtils.readLines(resource.getFile(), "UTF-8").toArray(), "\n");
        stubFor(get(urlPathEqualTo(url)).willReturn(aResponse().withBody(content).withStatus(200)));
    }

}
